package com.shanzmoo.db.order.entity.bo;

import cn.hutool.core.annotation.Alias;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.StrUtil;
import com.shanzmoo.base.constant.CharacterConstant;
import com.shanzmoo.base.exception.OutletBusinessException;
import com.shanzmoo.db.order.entity.dto.OutletOrderDetailSaveDto;
import lombok.Data;

import java.math.BigDecimal;

/**
 * 创建订单批量导入 excel 对应实体
 * @author Gzhao 2021/1/4
 */
@Data
public class OrderDetailSkuBO {

    @Alias("商品类型")
    private String type;

    @Alias("是否刷机单")
    private String root;

    @Alias("商品code")
    private String sku;

    @Alias("商品名称")
    private String skuName;

    @Alias("商品数量")
    private String skuNum;

    @Alias("商品单位")
    private String unit;

    @Alias("商品规格描述")
    private String attributeDesc;

    @Alias("商品总价")
    private String totalPrice;

    @Alias("商品SN码")
    private String snCode;

    public OutletOrderDetailSaveDto convert(Integer index) {

        if (!CharacterConstant.ZERO_STRING.equals(type) && !CharacterConstant.ONE_STRING.equals(type)){
            throw new OutletBusinessException("第" + index + "行，商品类型值传入错误");

        } else if (!NumberUtil.isNumber(skuNum)){
            throw new OutletBusinessException("第" + index + "行，商品数量值传入错误");

        } else if ( CharacterConstant.ZERO_STRING.equals(type) && !NumberUtil.isNumber(totalPrice) ){
            throw new OutletBusinessException("第" + index + "行，商品总价值传入错误");
        }

        OutletOrderDetailSaveDto orderDetailSaveDto = new OutletOrderDetailSaveDto();
        orderDetailSaveDto.setType(CharacterConstant.ONE_STRING.equals(type));
        orderDetailSaveDto.setRoot(CharacterConstant.ONE_STRING.equals(root));

        orderDetailSaveDto.setSku(sku.replace("-", ""));
        orderDetailSaveDto.setSkuName(skuName.replace("-", ""));
        orderDetailSaveDto.setSkuNum(Integer.parseInt(skuNum));
        orderDetailSaveDto.setUnit(unit.replace("-", ""));
        orderDetailSaveDto.setAttributeDesc(attributeDesc.replace("-", ""));

        if (CharacterConstant.ZERO_STRING.equals(type)){
            orderDetailSaveDto.setTotalPrice(new BigDecimal(totalPrice));
            orderDetailSaveDto.setUnitPrice(NumberUtil.div(totalPrice, skuNum).setScale(2));
        }
        if (StrUtil.isNotBlank(snCode)){
            snCode = snCode.replace("-", "").replace("null", "").trim();
        }
        orderDetailSaveDto.setSnCode(StrUtil.isBlank(snCode) ? "" : snCode);

        return orderDetailSaveDto;
    }
}
